package com.gxa.controller.commlife;

import com.gxa.common.utils.Result;
import com.gxa.entity.commlife.CommunityGuides;
import com.gxa.service.commlife.CommunityGuidesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:18:00
 */
@Api(tags = "社区导览")
@RestController
@RequestMapping("/community-guides")
public class CommunityGuidesController {
    @Autowired
    private CommunityGuidesService communityGuidesService;

//    List<CommunityGuides> communityGuidesList;
//    Date date = new Date("2022/11/11");
//    public CommunityGuidesController(){this.init();}
//    void init(){
//        communityGuidesList= new ArrayList<>();
//        CommunityGuides communityGuides = new CommunityGuides(1,"张三丰美食店","116°E,40°N","服务类",date,0);
//        communityGuidesList.add(communityGuides);
//        CommunityGuides communityGuides1 = new CommunityGuides(2,"张三丰美食店","116°E,40°N","服务类",date,0);
//        communityGuidesList.add(communityGuides1);
//    }


    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "查询社区导览")
    @GetMapping("list/{pageNum}/{pageSize}")
    public Result list(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
       try{
           CommunityGuides communityGuides = new CommunityGuides();
           communityGuides.setLocationName("");
           communityGuides.setType(null);
           Map<String, Object> map = this.communityGuidesService.queryList(communityGuides, pageNum, pageSize);
           return Result.success(map);
       }catch (Exception e){
           e.printStackTrace();
           return Result.failed("查询失败");
       }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @PostMapping("search")
    @ApiOperation(value = "搜索社区导览")
    public Result search(@RequestBody CommunityGuides communityGuides, Integer pageNum,Integer pageSize){
        try{
            Map<String, Object> map = this.communityGuidesService.queryList(communityGuides, pageNum, pageSize);
            Result<Map<String, Object>> result = Result.success(map);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("搜索失败");
        }
    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "增加社区导览")
    @PostMapping("add")
    public Result add(@RequestBody CommunityGuides communityGuides){
        try{
            long l = System.currentTimeMillis();
            Date date = new Date(l);
            communityGuides.setCreateTime(date);

            communityGuides.setLatitudeLongitude("116°E,40°N");
            this.communityGuidesService.addCommunityGuides(communityGuides);
            Result<Object> result = Result.success("添加成功");
            result.setMsg("添加成功");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("添加失败");
        }

    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "查询当前社区导览")
    @GetMapping("preEdit/{id}")
    public Result preEdit(@PathVariable("id") Integer id){
        try{
            CommunityGuides communityGuides = this.communityGuidesService.queryById(id);
            return Result.success(communityGuides);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("当前信息查询失败");
        }

    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation(value = "修改社区导览")
    @PutMapping("edit")
    public Result edit(@RequestBody CommunityGuides communityGuides){
        try{
            this.communityGuidesService.updateCommunityGuides(communityGuides);
            Result<Object> result = Result.success("修改成功");
            result.setMsg("修改成功");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("修改失败");
        }

    }

    @CrossOrigin(origins = "*",maxAge = 3600)//跨域请求配置
    @ApiOperation("删除社区导览")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        try{
            this.communityGuidesService.deleteCommunityGuides(id);
            Result<Object> result = Result.success("删除成功");
            result.setMsg("删除成功");
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("删除失败");
        }

    }

}
